<!--
 * @Author: hucc
 * @Date: 2021-10-14 11:00:29
 * @LastEditors: hucc
 * @LastEditTime: 2021-10-14 11:54:56
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <script src="../js/vue-router.js"></script>
    <script src='../js/jquery.js'></script>
    <style>
        .main {
            display: flex;
            height: 600px;
        }
        
        .left {
            border: 3px solid orange;
            width: 300px;
        }
        
        .right {
            flex-direction: row;
            border: 3px solid brown;
            width: calc(100% - 300px);
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="main">
            <div class="left">
                账号：<input type="text"><br> 密码：
                <input type="password"><br>
                <button @click='$router.push("/index")'>登录</button>
            </div>
            <div class="right">
                <router-view></router-view>

            </div>

        </div>
    </div>
    <script>
        let com1 = {
            data() {
                return {}
            },
            template: `
            <div>
              
                </div>
            `,
            created() {

            }
        }
        let com2 = {
            data() {
                return {}
            },
            template: `
            <div>
                主页
                </div>
            `
        }
        let router = new VueRouter({
            routes: [{
                path: '/login',
                component: com1,

            }, {
                path: '/index',
                component: com2,
                //配置路由独享守卫
                beforeEnter(to, from, next) {
                    //console.log('路由进入之前', to, from);
                    //next();
                    if (sessionStorage.getItem('token')) {
                        next();
                    } else {
                        next(false);
                    }

                }
            }]
        })
        $(function() {
            $('button').click(function() {
                let obj = {
                    username: 'admin1',
                    password: '123321'
                }
                $.ajax({
                    url: ' http://47.94.46.113:8888/user/login',
                    method: 'post',
                    data: JSON.stringify(obj),
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    success(res) {
                        console.log(res);
                        sessionStorage.setItem('token', res.data.token);
                    }
                })
            })
        })
        new Vue({
            el: "#app",
            data: {},
            methods: {

            },
            components: {},
            router: router
        })
    </script>
</body>

</html>